package com.jinke.uang.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jinke.uang.R;
import com.jinke.uang.bean.AuthUserInfoData;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 获取联系人的adapter
 */
public class ContactItemAdapter extends RecyclerView.Adapter<ContactItemAdapter.ContactViewHolder> {
    private Context context;
    private List<AuthUserInfoData.ContactsInfoBean.ContactsInfosBean> data;
    private OnSelectContactListener onSelectContactListener;
    private OnSelectRelationListener onSelectRelationListener;


    private boolean isOPPO;

    public void setModel(boolean model) {
        isOPPO = model;
        notifyDataSetChanged();
    }
    public ContactItemAdapter(Context context, List<AuthUserInfoData.ContactsInfoBean.ContactsInfosBean>
            data) {
        this.context = context;
        this.data = data;
    }

    @NonNull
    @Override
    public ContactViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_contact_layout, parent,
                false);
        return new ContactViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ContactViewHolder holder, int position) {
        holder.tvContact.setText(context.getResources().getString(R.string.contact_auth_contact)+(position+1));

        holder.etName.setText(data.get(position).getUserName());
        holder.etPhone.setText(data.get(position).getMobile());
        holder.etRelation.setText(data.get(position).getRelationValue());
        holder.etRelation.setTag(data.get(position).getRelation());

        holder.etName.setTag(data.get(position).getUserName());


        if (isOPPO) {
            holder.etPhone.setHint(context.getResources().getString(R.string.contact_auth_phone_hint_enter));
            holder.etPhone.setFocusable(true);
            holder.etPhone.setFocusableInTouchMode(true);
            holder.rlPhone.setOnClickListener(null);
            holder.ivPhone.setVisibility(View.INVISIBLE);
        } else {
            holder.ivPhone.setVisibility(View.VISIBLE);
            holder.rlPhone.setOnClickListener(v -> {
                if (onSelectContactListener != null && !isOPPO) {
                    onSelectContactListener.onClick(holder.rlPhone, holder.getLayoutPosition());
                }
            });

            holder.etPhone.setOnClickListener(v -> {
                if (onSelectContactListener != null&& !isOPPO) {
                    onSelectContactListener.onClick(holder.etPhone, holder.getLayoutPosition());
                }
            });
        }

        holder.rlRelation.setOnClickListener(v -> {
            if (onSelectRelationListener != null) {
                onSelectRelationListener.onClick(holder.etRelation, holder.getLayoutPosition());
            }
        });
        holder.etRelation.setOnClickListener(v -> {
            if (onSelectRelationListener != null) {
                onSelectRelationListener.onClick(holder.etRelation, holder.getLayoutPosition());
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }


    public interface OnSelectContactListener {
        void onClick(View view, int position);
    }

    /**
     * 联系人选择的
     *
     * @param onSelectContactListener
     */
    public void setOnSelectContactListener(OnSelectContactListener onSelectContactListener) {
        this.onSelectContactListener = onSelectContactListener;
    }


    public interface OnSelectRelationListener {
        void onClick(View view, int position);

    }

    /**
     * 关系选择的监听器
     *
     * @param onSelectRelationListener
     */
    public void setOnSelectRelationListener(OnSelectRelationListener onSelectRelationListener) {
        this.onSelectRelationListener = onSelectRelationListener;
    }


    static class ContactViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_contact)
        TextView tvContact;
        @BindView(R.id.etName)
        EditText etName;
        @BindView(R.id.rlName)
        RelativeLayout rlName;
        @BindView(R.id.etPhone)
        EditText etPhone;
        @BindView(R.id.ivPhone)
        ImageView ivPhone;
        @BindView(R.id.rlPhone)
        RelativeLayout rlPhone;
        @BindView(R.id.etRelation)
        TextView etRelation;
        @BindView(R.id.rlRelation)
        RelativeLayout rlRelation;

        ContactViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
